import {
  addMeetingRoom,
  queryMeetingRoom,
  updateMeetingRoom,
  deleteMeetingRoom,
} from '../services/MeetingRoomApi';

/**
 * space model
 * undefined api method
 */

export default {
  namespace: 'space',

  state: {
    loading: false,
    data: undefined,
  },

  effects: {
    *add({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(addMeetingRoom, payload);
      yield put({
        type: 'loadResponse',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    *fetch({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(queryMeetingRoom, payload);
      yield put({
        type: 'loadResponse',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    *update({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(updateMeetingRoom, payload);
      yield put({
        type: 'loadResponse',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    *delete({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(deleteMeetingRoom, payload);
      yield put({
        type: 'loadResponse',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
  },

  reducers: {
    changeLoading(state, { payload }) {
      return {
        ...state,
        loading: payload,
      };
    },
    loadResponse(state, { payload }) {
      return {
        ...state,
        data: payload,
      };
    },
  },
};
